/*
 * Zirco Browser for Android
 * 
 * Copyright (C) 2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.xtown.bingluo.xtieba.model.adapters;

import org.xtown.bingluo.xtieba.R;
import org.xtown.bingluo.xtieba.db.XtiebaAuthorDb;
import org.xtown.bingluo.xtieba.db.XtiebaPostsInfoDb;
import org.xtown.bingluo.xtieba.model.XtiebaAuthor;
import org.xtown.bingluo.xtieba.ui.activities.PostsInfoActivity;
import org.xtown.bingluo.xtieba.utils.DateUtils;
import org.xtown.bingluo.xtieba.utils.IOUtils;
import org.xtown.bingluo.xtieba.utils.LogUtlils;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class PostsInfoCursorAdapter extends SimpleCursorAdapter {
	
	private Context mContext;
	
	/**
	 * Constructor.
	 * @param context The context.
	 * @param layout The layout.
	 * @param c The Cursor. 
	 * @param from Input array.
	 * @param to Output array.
	 */
	public PostsInfoCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
		super(context, layout, c, from, to);
		mContext = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View superView = super.getView(position, convertView, parent);

		//图标
		ImageView thumbnailView = (ImageView) superView.findViewById(R.id.PostsRow_Thumbnail);
		
		String author = getCursor().getString(getCursor().getColumnIndex(XtiebaPostsInfoDb.AUTHOR));
		if (PostsInfoActivity.currentXtiebaPostsDb != null && !PostsInfoActivity.currentXtiebaPostsDb.author.equals(author)) {
			XtiebaAuthor o = XtiebaAuthorDb.getNetInstance(mContext).selectByName(author);
			String local_avatar = "";
			if (o != null && !o.local_avatar.equals("")) {
				local_avatar = o.local_avatar;
			}
			Bitmap bmp = IOUtils.makeAuthorAvatar(mContext, local_avatar);
			if (bmp != null) {
				thumbnailView.setImageBitmap(bmp);
			}
		} else {
			//目前先把头像
			thumbnailView.setVisibility(View.GONE);
		}
		
		//时间
		TextView dateView = (TextView) superView.findViewById(R.id.PostsRow_Date);
		int time = getCursor().getInt(getCursor().getColumnIndex(XtiebaPostsInfoDb.POSTS_CREATED_AT));
		dateView.setText(String.format(mContext.getString(R.string.PostsCursorAdapter_Publish), DateUtils.formatDateForUnixTimestamp(mContext, time)));
		
		superView.setTag(getCursor().getString(getCursor().getColumnIndex(XtiebaPostsInfoDb.ID)));
		
		return superView;
	}

}
